﻿using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;

namespace ScriptEase.Shell.NLog
{
    /// <summary>
    /// Log4Net Grid
    /// </summary>
    public partial class NLogControl : UserControl
    {
        public NLogControl()
        {
            InitializeComponent();
        }



        private void log4NetOutputLoad(object sender, EventArgs e)
        {
            NLogReceiver.Subscribe(new NLogReceiver.MessageReceiver(nLogMessageReceiver));
        }



        private NLogControlDataSet.DefaultDataTable GridDataTable
        {
            get { return this.controlDataSet.Default; }
        }


        private int _rowNo = 1;



        private void nLogMessageReceiver(string timeStamp, string level, string message, string threadId)
        {
            #region Thread synchronization


            if (this.ultraGrid1.InvokeRequired)
            {
                MethodInvoker del = () => nLogMessageReceiver(timeStamp, level, message, threadId);
                this.Invoke(del);
                return;
            }


            #endregion



            NLogControlDataSet.DefaultRow newRow = GridDataTable.NewDefaultRow();
            newRow.RowNo = _rowNo++;
            newRow.TimeStamp = timeStamp;
            newRow.Thread = threadId;
            newRow.Level = level;
            newRow.Text = message;
            GridDataTable.AddDefaultRow(newRow);

            setRowColor(newRow);

            if (updateTimer.Enabled) return;
            this.ultraGrid1.SuspendLayout();
            updateTimer.Start();
        }



        private void timer1Tick(object sender, EventArgs e)
        {
            #region Thread synchronization


            if (this.ultraGrid1.InvokeRequired)
            {
                MethodInvoker del = () => timer1Tick(sender, e);
                this.Invoke(del);
                return;
            }


            #endregion

            updateTimer.Stop();
            this.nLogControlControlDataSetBindingSource.MoveFirst();
        }



        private void setRowColor(NLogControlDataSet.DefaultRow gridDataRow)
        {
            if (ultraGrid1.Rows == null) return;    // on end application

            UltraGridRow gridRow = getUltraGridRow(gridDataRow);

            switch (gridDataRow.Level)
            {
                case "ERROR":
                    gridRow.Appearance.ForeColor = Color.Red;
                    break;
                case "WARN":
                    gridRow.Appearance.ForeColor = Color.Orchid;
                    break;
            }
        }



        private UltraGridRow getUltraGridRow(object gridDataRow)
        {
            return ultraGrid1.Rows.FirstOrDefault(ugr => gridDataRow.Equals(getDataRow(ugr)));
        }



        private NLogControlDataSet.DefaultRow getDataRow(UltraGridRow ultraGridRow)
        {
            return getDataRow(ultraGridRow.ListObject);
        }



        private NLogControlDataSet.DefaultRow getDataRow(object listObject)
        {
            return getDataRow((DataRowView)listObject);
        }



        private NLogControlDataSet.DefaultRow getDataRow(DataRowView rowView)
        {
            return (NLogControlDataSet.DefaultRow)rowView.Row;
        }
    }
}